<?php

/**
 * Class Clander
 * php日历
 * @author:xyb
 * @date:2014/11/17
 */
class Clander extends CWidget {

    // 年份
    private $year;
    // 月份
    private $month;
    // 日期
    private $day;
    // 星期几
    private $week;

    // 中文的星期几对应数组
    private $zhWeek = array(
        0 => '星期日',
        1 => '星期一',
        2 => '星期二',
        3 => '星期三',
        4 => '星期四',
        5 => '星期五',
        6 => '星期六',
    );

    // 覆盖方法
    public function run()
    {
        // 获取当时的年月日
        $this->year = date('Y');
        $this->month = date('m');
        $this->day = date('d');
        $this->week = date('w');

        echo $this->makeHtml();
    }

    // 根据当前月份获取月份的日数，2月份比较特殊 因此另外处理
    private function getDaysOfMonth($month)
    {
        $isLeep = $this->isLeap();
        $febDays = $isLeep === true ? 29 : 28;

        if(substr($month, 0, 1) == '0')
            $month = substr($month,1);

        $daysOfMonth = array(
            01=>31,
            2=>$febDays,
            3=>31,
            4=>30,
            5=>31,
            6=>30,
            7=>31,
            8=>31,
            9=>30,
            10=>31,
            11=>30,
            12=>31
        );

        return $daysOfMonth[ $month ];
    }

    /**
     * @return boolean 是否闰年
     */
    private function isLeap()
    {
        /**
         * 判断闰年
         * 1、年数除以四，能整除的就是闰年。
         * 2、如果能被100整除的话，就要被400整除才能成为闰年。
         */
        if (
            (($this->year / 100 == 0) && ($this->year / 400 == 0))
            ||
            (($this->year / 4 == 0) && ($this->year / 100 != 0))
        )
            return true;
        else
            return false;
    }

    /**
     * 获取中文的星期几
     * @return string 星期几
     */
    private function getZhWeek()
    {
        return $this->zhWeek[ $this->week ];
    }

    /**
     * 获取当月一号是星期几
     * @return int
     */
    private function getFirstIsWeek()
    {
        $date = $this->year.'-'.$this->month.'-1';
        $time = strtotime($date);
        return date('w', $time);
    }

    /**
     * 创建日历html
     * @return string
     */
    private function makeHtml()
    {
        $html = '';

        $html .= <<<EOP
<p>　　　{$this->year} 年 {$this->month} 月 {$this->day} 日   　　　　　{$this->getZhWeek()}</p>
<table class="table-bordered calendar-table">
    <tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>
EOP;

        $td = '';

        $days = $this->getDaysOfMonth($this->month);

        $html .= '<tr>';

        // 获得当月1号是星期几
        $firstDayWeek = $this->getFirstIsWeek();

        // 根据当前是星期几，推算应该隔开多少天
        $diff = $firstDayWeek - 0;

        for($i=0; $i < $diff; $i++)
        {
            $td .= '<td></td>';
        }

        for($i=1; $i<=$days; $i++)
        {
            $today = $this->day != $i ? '' : 'class="active"';

            if(($i+$diff) % 7 != 0)
                $td .= "<td $today>$i</td>";
            elseif($i == sizeof($days) - 1)
                $td .= "<td $today>$i</td></tr>";
            else
                $td .= "<td $today>$i</td></tr><tr>";
        }

        // 填充剩余空格
        $left = ($days+$diff) % 7;

        for($i=0; $i<7-$left; $i++)
        {
            $td .= '<td></td>';
        }
        $html .= $td;
        $html .= '</table>';

        return $html;
    }
}
